#include "user.h"
#include "ui_user.h"
#include "lib/sql.h"
#include "dlg_ueradd.h"
#include<QMessageBox>
user::user(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::user)
{
    ui->setupUi(this);
    ui->tableView->setModel(&m_model);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

}

user::~user()
{
    delete ui;
}

void user::initpage(QString strCondition)
{
   //查询数据库并显示
    auto l=Sql::getinstance()->getuser(strCondition);
    m_model.clear();
    m_model.setHorizontalHeaderLabels(QStringList{"用户id","姓名","账户名称","密码","联系电话","所属管理员"});

    for(int i=0;i<l.size();i++){
        QList<QStandardItem*> items;
        for(int j=0;j<l[i].size();j++)
        {
            items.append(new QStandardItem(l[i][j]));
        }
        m_model.appendRow(items);
   }
}

void user::on_import_user_clicked()
{
    dlg_ueradd dlg;
    dlg.exec();
    initpage();
}


void user::on_delete_user_clicked()
{
    int r=ui->tableView->currentIndex().row();
    if(r<0)
    {
        QMessageBox::information(nullptr,"信息","无法选中用户");
    }
    else
    {
        auto id = m_model.item(r,0)->text();
        Sql::getinstance()->deuser(id);
        initpage();
    }
}


void user::on_search_user_textChanged(const QString &arg1)
{
    QString strcond =QString("where name like '%%1%'").arg(arg1);
    initpage(strcond);
}


void user::on_changed_user_clicked()
{
    int r = ui->tableView->currentIndex().row();
    if(r < 0){
        QMessageBox::information(nullptr,"信息","无选择用户");
    }
    else{
        auto id = m_model.item(r,0)->text();
        dlg_ueradd dlg;
        dlg.types(id.toInt());
        dlg.exec();
        initpage();
    }
}



